package com.dongdong.config.log;

import com.alibaba.fastjson.JSONObject;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

//日志切面类
@Component
@Aspect //表示当前类为切面类
public class LogAspect {

   public Logger logger = LoggerFactory.getLogger(LogAspect.class);

    @Pointcut("execution(public * com.dongdong.service.*.*(..))")
    public void aopLog(){
    }
    @Before("aopLog()")
    public void doBefore(JoinPoint joinPoint){
        Object[] args = joinPoint.getArgs(); //调用每一个方法的参数
        Signature signature = joinPoint.getSignature();

        logger.info("正在执行的方法 : {},参数为: {}" ,signature,args);
    }
    //后置通知
    @AfterReturning(pointcut = "aopLog()",returning ="result" )
    public void doAfter(Object result)throws Throwable{
                                                    //通过json转成字符串
        logger.info("方法执行结束 , 返回值为 : {}", JSONObject.toJSONString(result));

    }


}
